package ntd.util.util;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.StaticLog;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import lombok.Data;
import lombok.SneakyThrows;

public class UtilTest {

    
    public static void main(String[] args) {
        BigDecimal allPrice = new BigDecimal(0);
        for (int t = 0; t < 100; t++) {
            BigDecimal countPrice = new BigDecimal(0);
            for (int j = 0; j < 1000; j++) {
                MoneyBo moneyBo = new MoneyBo();
                int i = 0;
                for (; i < 50; i++) {
                    moneyBo.bd();
                }
                countPrice = countPrice.add(moneyBo.printPrice());
            }
            StaticLog.info(countPrice.toString());
            allPrice = allPrice.add(countPrice);
        }
        StaticLog.info(allPrice.toPlainString());
    }
}

@Data
class MoneyBo {

    // 单股价格
    private BigDecimal bfb = new BigDecimal(1);

    // 上次购买跌幅点
    private BigDecimal dbfb = new BigDecimal(1);

    // 本金
    private BigDecimal money = new BigDecimal(2000);

    // 股数
    private BigDecimal num = new BigDecimal(2000);

    // 次数统计
    private Map<String, Integer> integerMap = new HashMap<>();

    public BigDecimal printPrice() {
        BigDecimal yPrice = this.getNum().multiply(this.getBfb()).subtract(this.getMoney());
        BigDecimal divide = yPrice.divide(this.getMoney(), RoundingMode.HALF_DOWN);
//        StaticLog.info("市场波动结果, 百分比[{}], 本金[{}], 股数[{}], 盈利[{}], 盈利百分比[{}]", this.getBfb(), this.getMoney(), this.getNum(),
//                yPrice, divide);
        return yPrice;
    }

    // 波动
    public void bd() {
        if (RandomUtil.randomBoolean()) {
            if (bfb.compareTo(new BigDecimal(1.7)) < 0) {
                // 涨
                bfb = bfb.add(new BigDecimal(0.05)).setScale(5, RoundingMode.HALF_DOWN);
            }
            // 抛售规则
            if (bfb.compareTo(new BigDecimal(1.3)) > 0) {
                // 卖出份额
                BigDecimal tnum = num.multiply(new BigDecimal(0.1)).setScale(5, RoundingMode.HALF_DOWN);
                // 份额减少
                num = num.subtract(tnum).setScale(5, RoundingMode.HALF_DOWN);
                // 本金减少
                money = money.subtract(tnum.multiply(bfb).setScale(5, RoundingMode.HALF_DOWN)).setScale(5, RoundingMode.HALF_DOWN);
            } else {
                Integer orDefault = integerMap.getOrDefault(bfb.toPlainString() + "true", 0);
                if (orDefault < 1) {
                    money = money.add(new BigDecimal(1000)).setScale(5, RoundingMode.HALF_DOWN);
                    num = num.add(new BigDecimal(1000).divide(bfb, RoundingMode.HALF_DOWN)).setScale(5, RoundingMode.HALF_DOWN);
                    integerMap.put(bfb.toPlainString() + "true", orDefault + 1);
                }
            }
            dbfb = bfb;
        } else {
            if (bfb.compareTo(new BigDecimal(0.3)) > 0) {
                // 跌
                bfb = bfb.add(new BigDecimal(-0.05)).setScale(5, RoundingMode.HALF_DOWN);

                Integer orDefault = integerMap.getOrDefault(bfb.toPlainString() + "false", 0);
                if (orDefault < 2) {
                    if (bfb.compareTo(new BigDecimal(1)) < 1 || dbfb.subtract(bfb).compareTo(new BigDecimal(0.9)) > 0) {
                        money = money.add(new BigDecimal(2000)).setScale(5, RoundingMode.HALF_DOWN);
                        num = num.add(new BigDecimal(2000).divide(bfb, RoundingMode.HALF_DOWN)).setScale(5, RoundingMode.HALF_DOWN);
                        integerMap.put(bfb.toPlainString() + "false", orDefault + 1);
                        dbfb = bfb;
                    }
                }

            }
        }
//        this.printPrice();
//        JSONObject jsonObject = JSONUtil.parseObj(this);
//        jsonObject.remove("integerMap");
//        StaticLog.info(jsonObject.toString());
    }

}
